﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace WPFDataGridPaging {
    public class FakeDatabase {
        public int ID {
            get; set;
        }
        public string Event {
            get; set;
        }
        public string Time {
            get; set;
        }
        public string RealName {
            get; set;
        }
        public string Type {
            get; set;
        }
        public string Note {
            get; set;
        }

        //public List<FakeDatabase> GenerateFakeSource() {
        //    List<FakeDatabase> source = new List<FakeDatabase>();

        //    for(int i = 0;i < 1000;i++) {
        //        FakeDatabase item = new FakeDatabase() {
        //            Id = i,
        //            ItemName = "Item" + i.ToString()
        //        };

        //        source.Add(item);
        //    }

        //    return source;
        //}

        public List<FakeDatabase> DataTableSource() {
            List<FakeDatabase> sourcelist = new List<FakeDatabase>();

            DataTable source = new DataTable("Datas");
            DataColumn dc = null;

            dc = source.Columns.Add("ID",System.Type.GetType("System.Int32"));
            dc.AutoIncrement = true;//自动增加
            dc.AutoIncrementSeed = 1;//起始为1
            dc.AutoIncrementStep = 1;//步长为1
            dc.AllowDBNull = false;//

            source.Columns.Add("Event",System.Type.GetType("System.String"));
            source.Columns.Add("Time",System.Type.GetType("System.String"));
            source.Columns.Add("RealName",System.Type.GetType("System.String"));
            source.Columns.Add("Type",System.Type.GetType("System.String"));
            source.Columns.Add("Note",System.Type.GetType("System.String"));
            DataRow newRow;
            for(int i = 0;i < 100;i++) {
                newRow = source.NewRow();
                newRow["Event"] = "2";
                newRow["Time"] = "2";
                newRow["RealName"] = "/";
                newRow["Type"] = "/";
                newRow["Note"] = "/";
                source.Rows.Add(newRow);
            }

            //sourcelist = ModelConvertHelper<FakeDatabase>.ConvertToModel(source) as List<FakeDatabase>;

            sourcelist = ModelConvertHelper.ToList<FakeDatabase>(source) as List<FakeDatabase>;

            return sourcelist;
        }

        public FakeDatabase() {

        }
    }

    public class ModelConvertHelper<T> where T : new()  // 此处一定要加上new()
    {
        public static IList<T> ConvertToModel(DataTable dt) {
            IList<T> ts = new List<T>();// 定义集合
            Type type = typeof(T); // 获得此模型的类型
            string tempName = "";
            foreach(DataRow dr in dt.Rows) {
                T t = new T();
                PropertyInfo[] propertys = t.GetType().GetProperties();// 获得此模型的公共属性
                foreach(PropertyInfo pi in propertys) {
                    tempName = pi.Name;
                    if(dt.Columns.Contains(tempName)) {
                        if(!pi.CanWrite)
                            continue;
                        object value = dr[tempName];
                        if(value != DBNull.Value)
                            pi.SetValue(t,value,null);
                    }
                }
                ts.Add(t);
            }
            return ts;
        }
    }

    public class ModelConvertHelper {
        /// <summary>    
        /// DataTable 转换为List 集合    
        /// </summary>    
        /// <typeparam name="TResult">类型</typeparam>    
        /// <param name="dt">DataTable</param>    
        /// <returns></returns>    
        public static List<T> ToList<T>(DataTable dt) where T : class, new() {
            List<PropertyInfo> prlist = new List<PropertyInfo>();
            Type t = typeof(T);
            Array.ForEach<PropertyInfo>(t.GetProperties(),p => {
                if(dt.Columns.IndexOf(p.Name) != -1)
                    prlist.Add(p);
            });
            List<T> oblist = new List<T>();
            foreach(DataRow row in dt.Rows) {
                T ob = new T();
                prlist.ForEach(p => {
                    if(row[p.Name] != DBNull.Value)
                        p.SetValue(ob,row[p.Name],null);
                });
                oblist.Add(ob);
            }
            return oblist;
        }
    }
}
